//
//  ViewController.m
//  UI_25_homework_GCD
//
//  Created by li cheng on 16/8/17.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 并行
//    [self parallelingGCD];
    // 异步串行
//    [self serialAsync];
    // 用同步函数往并发队列中添加任务
//    [self concurrenceSynchr];
    // 用同步函数往串行队列中添加任务
//    [self concurrenceSynchr];
}
#pragma mark - 并行
- (void)parallelingGCD{
    //1.获得全局的并发队列
        dispatch_queue_t queue =  dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //2.添加任务到队列中，就可以执行任务
    //异步函数：具备开启新线程的能力
    dispatch_async(queue, ^{
    NSLog(@"下载图片1----%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
    NSLog(@"下载图片2----%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
    NSLog(@"下载图片2----%@",[NSThread currentThread]);
    });
    //打印主线程
    NSLog(@"主线程----%@",[NSThread mainThread]);
}
#pragma mark - 异步串行
- (void)serialAsync{
    //打印主线程
    NSLog(@"主线程----%@",[NSThread mainThread]);
    //创建串行队列
    dispatch_queue_t  queue= dispatch_queue_create("wendingding", NULL);
    //第一个参数为串行队列的名称，是c语言的字符串
    //第二个参数为队列的属性，一般来说串行队列不需要赋值任何属性，所以通常传空值（NULL）
    //2.添加任务到队列中执行
    dispatch_async(queue, ^{
    NSLog(@"下载图片1----%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
    NSLog(@"下载图片2----%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
    NSLog(@"下载图片2----%@",[NSThread currentThread]);
    });
    //3.释放资源
    //    dispatch_release(queue);
    
}
#pragma mark - 用同步函数往并发队列中添加任务
- (void)concurrenceSynchr{
    //打印主线程
    NSLog(@"主线程----%@",[NSThread mainThread]);
    //创建串行队列
    dispatch_queue_t  queue= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //2.添加任务到队列中执行
    dispatch_sync(queue, ^{
    NSLog(@"下载图片1----%@",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
    NSLog(@"下载图片2----%@",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
    NSLog(@"下载图片3----%@",[NSThread currentThread]);
    });
}
#pragma mark - 用同步函数往串行队列中添加任务
- (void)serialSynchr{
    NSLog(@"用同步函数往串行队列中添加任务");
    //打印主线程
    NSLog(@"主线程----%@",[NSThread mainThread]);
    //创建串行队列
    dispatch_queue_t  queue= dispatch_queue_create("wendingding", NULL);
    //2.添加任务到队列中执行
    dispatch_sync(queue, ^{
    NSLog(@"下载图片1----%@",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
    NSLog(@"下载图片2----%@",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
    NSLog(@"下载图片3----%@",[NSThread currentThread]);
    });
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
